package algorithm.leetcode.I601to800;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Q696 {
    public int countBinarySubstrings(String s) {
        int result = 0;

        // targetIndex记录的是字母发生变化的位置,并头尾添加0和s长度,即 [0, ... , s.length()]
        ArrayList<Integer> targetIndex = new ArrayList<>(Arrays.asList(0));
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i-1) != s.charAt(i)) targetIndex.add(i);
        }
        targetIndex.add(s.length());

        for (int i = 1; i < targetIndex.size()-1; i++) {
            int maxSideLength = Math.min(targetIndex.get(i)-targetIndex.get(i-1), targetIndex.get(i+1)-targetIndex.get(i));
            result += maxSideLength;
        }
        return result;
    }

    public static String switchStr(String s) {
        return s.charAt(0) == '0' ? new String(new char[s.length()]).replace("\0", "1") : new String(new char[s.length()]).replace("\0", "0");
    }


    public static <T> void vistor(ArrayList<T> a){
        Iterator<T> iterator = a.iterator();
        while(iterator.hasNext()){
            T t = iterator.next();
            System.out.println(t);
        }
    }

    public static void main(String[] args) {
        Q696 snx = new Q696();
        String s = "10101";
        int a = snx.countBinarySubstrings(s);
        System.out.println(a);
    }

}

